package javabasic.singleton.lanhanshi;

import jdk.nashorn.internal.codegen.CompilerConstants;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 静态内部类实现懒汉式单例模式
 */
public class Singleton06 {
    private Singleton06(){
        System.out.println("创建对象");
    }
    /*
    在静态内部类被加载和初始化时，才创建实例
    静态内部类 不会 随着外部类的加载和初始化而加载和初始化
     */
    private static class Inner{
        private static Singleton06 INSTANCE=new Singleton06();
    }

    public static Singleton06 getInstance(){
        return Inner.INSTANCE;
    }

    public static void main(String[] args) throws Exception{
        Callable<Singleton06> singleton06Callable = new Callable<Singleton06>() {
            @Override
            public Singleton06 call() throws Exception {
                return Singleton06.getInstance();
            }
        };
        ExecutorService threadPool = Executors.newFixedThreadPool(2);
        Future<Singleton06> future = threadPool.submit(singleton06Callable);
        Future<Singleton06> future1 = threadPool.submit(singleton06Callable);
        Singleton06 singleton06 = future.get();
        Singleton06 singleton061 = future1.get();
        System.out.println(singleton06==singleton061);
        threadPool.shutdown();
    }
}
